﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Runtime.InteropServices.WindowsRuntime;
using System.Text;
using System.Threading.Tasks;

namespace 特性
{
    class Utils
    {
        #region 使用特性类的过程

        //验证过程
        //1.通过映射，找到成员属性关联的特性类实例，
        //2.使用特性类实例对新对象的数据进行验证

        //用特性类验证订单号长度
        public static bool isIDLengthValid(int IDLength, MemberInfo member)
        {
            foreach (object attribute in member.GetCustomAttributes(true)) //2.通过映射，找到成员属性上关联的特性类实例，
            {
                if (attribute is StringLengthAttribute)//3.如果找到了限定长度的特性类对象，就用这个特性类对象验证该成员
                {
                    StringLengthAttribute attr = (StringLengthAttribute)attribute;
                    if (IDLength < attr.MinLength || IDLength > attr.MaxLength)
                    {
                        string displayName = attr.DisplayName;
                        int maxLength = attr.MaxLength;
                        int minLength = attr.MinLength;
                        string error = attr.ErrorMessage;
                        Console.WriteLine(error, displayName, maxLength, minLength);//验证失败，提示错误
                        return false;
                    }
                    else return true;
                }

            }
            return false;
        }


        //验证订单对象是否规范
        public static bool isOrderValid(Order order)
        {
            if (order == null) return false;

            // 用类名来进行反射操作
            Type type = typeof(Order);

            PropertyInfo[] propertyInfos = type.GetProperties();
            MethodInfo[] methodInfos = type.GetMethods();
            MemberInfo[] memberInfos = type.GetMembers();

            foreach (MemberInfo m in memberInfos)
            {
                if (isIDLengthValid(order.OrderID.Length, m))//1记录下新对象需要验证的数据，
                    return true;
            }


            //foreach (PropertyInfo p in propertyInfos)
            //{
            //    if (isIDLengthValid(order.OrderID.Length, p))//1记录下新对象需要验证的数据，
            //        return true;
            //}
            return false;

        }
        #endregion

        #region 显示开发人员信息

        /// <summary>
        /// 显示程序的修改信息
        /// </summary>
        /// <param name="member"></param>
        /// <returns></returns>
        public static string ShowWriteInfo(MemberInfo member)
        {
            // 成员的名字：方法名，类名，字段名
            StringBuilder sb = new StringBuilder();
            MemberTypes memberType = member.MemberType;
            if (memberType == MemberTypes.Method)
            {
                // append 和 appendline的区别，就是后者会换行
                sb.Append("方法名：").AppendLine(member.Name);
                // IEnumerable<Attribute> enumerable = member.GetCustomAttributes();// 如果只有一个特性，就用这个方法
                // IEnumerable<WriterInfo> enumerable = member.GetCustomAttributes<WriterInfo>();
                sb.AppendLine(getWriterInfo(member));
            }
            else if(memberType == MemberTypes.TypeInfo)
            {
                sb.Append("类名:").AppendLine(member.Name);
                sb.AppendLine(getWriterInfo(member));
            }
            else if(memberType == MemberTypes.Field)
            {
                sb.Append("字段:").AppendLine(member.Name);
                sb.AppendLine(getWriterInfo(member));
            }
            else if(memberType == MemberTypes.Property)
            {
                sb.Append("属性:").AppendLine(member.Name);
                sb.AppendLine(getWriterInfo(member));
            }
            return sb.ToString();
        }

        /// <summary>
        /// 获取程序修改的信息
        /// </summary>
        /// <param name="member"></param>
        /// <returns></returns>
        private static string getWriterInfo(MemberInfo member)
        {
            StringBuilder sb = new StringBuilder();
            List<WriterInfo> writerInfos = member.GetCustomAttributes<WriterInfo>().ToList();
            if (writerInfos.Count > 0)
            {
                foreach (WriterInfo item in writerInfos)
                {
                    sb.AppendLine(item.Info());
                }
            }
            return sb.ToString();
        }

        /// <summary>
        /// 编辑信息
        /// </summary>
        /// <param name="type"></param>
        /// <returns></returns>
        public static string editMsg(Type type)
        {
            StringBuilder sb = new StringBuilder();
            sb.Append("类名:").AppendLine(type.FullName);
            sb.AppendLine(getWriterInfo(type));
            return sb.ToString();
        }
        #endregion
    }
}
